package module.admin;

import bean.Admin;
import bean.Constant;
import module.ahleteView.AthleteView;
import util.UI.SwingUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @Author leiHua
 * @Date 2022/5/24 00:32
 */
public class AdminView extends JFrame {
    public static int jframeHeight = 920;  //窗口高度
    public static int jframeWidth = 1400;   //窗口宽度
    public static TrayIcon trayicon;// 系统托盘图标
    public static JLayeredPane contentLayered=null;
    JFrame jFrame;
    public AdminView() {
        SwingUI.setUI();
        jFrame = new JFrame();
        jFrame.setLayout(null);
        //窗口背景图片
        ImageIcon backimg = new ImageIcon("src/imag/admin.jpg");
        backimg.setImage(backimg.getImage().getScaledInstance(jframeWidth, jframeHeight, Image.SCALE_DEFAULT));//缩放图片
        JLabel imgJlabel = new JLabel(backimg);
        JPanel jPanel1 = new JPanel();
        jPanel1.add(imgJlabel);
        jPanel1.setBounds(0, 0, jframeWidth, jframeHeight);
        jFrame.getLayeredPane().add(jPanel1, 0);


        jFrame.setBounds((Constant.SCREEN_WIDETH - jframeWidth) / 2, (Constant.SCREEN_HEIGHT - jframeHeight) / 2, jframeWidth, jframeHeight);  //设置窗口位置
        jFrame.setTitle("运动会后台管理");
        Image image = new ImageIcon("src/imag/logo.jpg").getImage();
        jFrame.setIconImage(image);

        //创建导航头
        AdminHead adminHead = new AdminHead();
        jFrame.getLayeredPane().add(adminHead, new Integer(Integer.MAX_VALUE));
        //创建侧边栏
        AdminSidebar adminSidebar = new AdminSidebar();
        jFrame.getLayeredPane().add(adminSidebar, new Integer(Integer.MAX_VALUE));
        //创建内容容器：
        AdminContent adminContent = new AdminContent();
        contentLayered = jFrame.getLayeredPane();
        //1、将所有的Admincontent的内容都加入到 当前分层容器中
        //2、选中某个结点时 ，其他的内容Jpanel都设置为不可见， 只显示 对应结点的Jpanal
        //3、一开始默认显示起始页
        contentLayered.add(adminContent.getinitContent(), new Integer(3));
        contentLayered.add(adminContent.getCompetitionPanel(), new Integer(1));
        contentLayered.add(adminContent.getCheckPanel(), new Integer(1));
        contentLayered.add(adminContent.getWritePanel(), new Integer(1));
        contentLayered.add(adminContent.getAthletePanel(), new Integer(1));
        contentLayered.add(adminContent.getUpdateUrlPanel(),new Integer(1));
        adminContent.getCompetitionPanel().setVisible(false);
        adminContent.getCheckPanel().setVisible(false);
        adminContent.getWritePanel().setVisible(false);
        adminContent.getAthletePanel().setVisible(false);
        adminContent.getUpdateUrlPanel().setVisible(false);
        jFrame.setSize(jframeWidth, jframeHeight);
        jFrame.setVisible(true);


        //系统托盘
        jFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        jFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                systemTrayInitial();
                jFrame.setVisible(false);
            }
        });

    }

    /**
     * 创建系统托盘
     */
    private void systemTrayInitial() {
        if (!SystemTray.isSupported()) // 判断当前系统是否支持系统栏
            return;
        try {
            String title = "广软运动会系统";// 系统栏通知标题
            String personal = "雷诏华";// 系统通知栏内容
            SystemTray sysTray = SystemTray.getSystemTray();// 获取系统默认托盘
            Image image = Toolkit.getDefaultToolkit().getImage(AthleteView.class.getResource("/imag/logo.jpg"));// 创建系统栏图标
            trayicon = new TrayIcon(image, title + "\n" + personal, createMenu());//创建系统栏图标对象
            trayicon.setImageAutoSize(true);    //设置自动大小
            trayicon.addActionListener(new ActionListener() {   //最小化托盘的双击事件
                @Override
                public void actionPerformed(ActionEvent e) {
                    jFrame.setVisible(true);
                    jFrame.toFront();
                }
            });//添加事件监听器
            sysTray.add(trayicon);//添加系统栏图标到系统托盘
            trayicon.displayMessage(title, personal, TrayIcon.MessageType.INFO);    //显示系统栏图标提示文本
            this.setVisible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建系统托盘的最小化菜单栏
     *
     * @return
     */
    private PopupMenu createMenu() {
        PopupMenu menu = new PopupMenu();
        MenuItem exitItem = new MenuItem("exit");
        exitItem.addActionListener(new ActionListener() { // 系统栏退出事件
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        MenuItem openItem = new MenuItem("open");
        openItem.addActionListener(new ActionListener() {// 系统栏打开菜单项事件
            public void actionPerformed(ActionEvent e) {
                if (!isVisible()) {    //如果主窗体隐藏
                    jFrame.setVisible(true);//显示主窗体
                    jFrame.toFront();    //使主窗体显示在最上层
                } else
                    jFrame.toFront();    //使主窗体显示在最上层
            }
        });
        menu.add(openItem);//添加打开菜单项
        menu.addSeparator();//添加菜单项分隔符
        menu.add(exitItem);//添加退出菜单项
        return menu;
    }

    public static void main(String[] args) {
        new AdminView();
    }
}
